using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
using System.Text;
using System.IO;
using System.Collections;
using Microsoft.Win32;
using Microsoft.VisualBasic;

public partial class Regression : System.Web.UI.Page
{
    //int countResult = 0;
    //string gsAllTransactions = "";
    //int liLnkPos = 0;
    //string lsLinkClientId = "";
    //string liRowId = "";
    //string lsColumnData = "";
    //string lsTransactonName = "";
    //bool isReady4InsertData = false;
    SqlDataReader sDRdr;
    DataSet dsMenu = new DataSet();
    DataSet dsField = new DataSet();
    SQLInteract objSQL = new SQLInteract();
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
        Response.Expires = -1500;
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.CacheControl = "no-cache";
        //if (Session["UserName"] == null) //Redirect to login if session expires
        //{
        //    Response.Redirect("Login.aspx", true);
        //}

        lblFlowType.Text = Session["PageFlow"].ToString().Trim();
        Master.Page.Title = "GSTAR-" + Session["PageFlow"].ToString().Trim();
        
        try
        {
            string queryDelete = "Delete from Transaction_Tb where UsrId='" + Session["UserId"].ToString() + "' and FlowType='" + Session["FlowType"].ToString().Trim() + "'"+
                                "Delete from TEST_CASE_SELECTION_SHEET_GEN_TB where UserId='" + Session["UserId"].ToString() + "' and FlowType='" + Session["FlowType"].ToString().Trim() + "'";
            objSQL.sqlExecuteNonQuery(queryDelete);
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" > alert(\"Error: " + ex.Message + "\");</script>", false);
        }
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {

    }
    protected void doAddRow(object sender, EventArgs e)
    {
        if (txtTestCases.Text.Trim().ToString() == "0" || txtTestCases.Text.Trim().ToString() == "")
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" > alert(\"Error: Number of test cases should be greater than 0.\");</script>", false);
        }
        //get menu dataset
        string menuQ = "select [BUS_TYPE]  FROM [AvivaTPortal].[dbo].[Menu] where [MenuID]=" + Session["FlowType"] + "";
        dsMenu = objSQL.ReturnDataAdapter(menuQ);
        string fieldtype = dsMenu.Tables[0].Rows[0][0].ToString();
        //get Fields in menu dataset
        dsField = objSQL.ReturnDataAdapter("SELECT FID,FTEXT FROM [AvivaTPortal].[dbo].[TC_FEILDS] where [BUS_TYPE]='" + fieldtype + "' ");
         
        int rowCount = Convert.ToInt32(txtTestCases.Text);
        string TotalTC = "Select count(*) from Transaction_Tb" +
                "where UsrId='" + Session["UserId"].ToString() + "' and FlowType='" + Session["FlowType"].ToString() + "'";
        int CountTC =int.Parse(objSQL.sqlExecuteScalar(TotalTC));
                   
        if (CountTC>0)
        {
            //delete record from transation_tb and insert new record
            deleteTCData(dsField);
            InsertTCData(dsField);
            CreateFieldRow();
        }
        else
        {
            InsertTCData(dsField);
            CreateFieldRow();
        }
    }
    public void deleteTCData(DataSet fieldDS)
    {

        StringBuilder sbDeleteQry = new StringBuilder("");
        foreach (DataRow row in fieldDS.Tables[0].Rows)
        {
            for (int i = 1; i <= Int32.Parse(txtTestCases.Text); i++)
            {
                sbDeleteQry.Append("Delete from Transaction_Tb where UsrId='" + Session["UserId"].ToString() + "'and FlowType='" + Session["FlowType"].ToString().Trim() + "'" +
                                "Delete from TEST_CASE_SELECTION_SHEET_GEN_TB where UserId='" + Session["UserId"].ToString() + "' and FlowType='" + Session["FlowType"].ToString().Trim() + "'");
                
            }
        }
        objSQL.sqlExecuteNonQuery(sbDeleteQry.ToString());
    }
    public void InsertTCData(DataSet fieldDS)
    {
        StringBuilder sbInsertQry = new StringBuilder("");
        foreach (DataRow row in fieldDS.Tables[0].Rows)
        {
            for (int i = 1; i <= Int32.Parse(txtTestCases.Text); i++)
            {
                sbInsertQry.Append( "Insert into Transaction_Tb([TestCase],[TransactionText],[UsrId],[SrlNmbr],[UpdateInd],[FlowType],[FeildID]) values('TC" + i.ToString() + "','-','" + Session["UserId"].ToString() + "'," + i.ToString() + ",'N','" + Session["FlowType"].ToString().Trim() + "','" + row[1].ToString().Trim() + "')" +
                                     "Insert into TEST_CASE_SELECTION_SHEET_GEN_TB values('TC" + i.ToString() + "','','','','','','" + Session["FlowType"].ToString().Trim() + "','" + Session["UserId"].ToString() + "',0)");
                
            }
        }
        objSQL.sqlExecuteNonQuery(sbInsertQry.ToString());
    }
    public void CreateFieldRow()
    {
        Table DynTable = new Table();
        DynTable.ID = "TbTestCaseTransactions";
        DynTable.BorderWidth = 0;
        DynTable.CellPadding = 0;
        DynTable.CellPadding = 0;
        DynTable.CssClass = "TCtableborder";
        DynTable.BackColor = System.Drawing.Color.PaleGoldenrod;
        string lsRowUpdateStatusStr = "";
        try
        {
            string TCRecord = "Select distinct [TestCase],SrlNmbr,UpdateInd from Transaction_Tb" +
                "where UsrId='" + Session["UserId"].ToString() + "' and FlowType='" + Session["FlowType"].ToString() + "'";
            sDRdr = objSQL.ReturnDataReader(TCRecord);
            if (sDRdr.HasRows)
            {

            }
        }
        catch (Exception ex)
        {
            //sDRdr.Close();
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
        }
    }
}
